<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: 李学健
  Date: 2024/3/10
  Time: 11:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String username, password;
    //解析请求前加上这一句，传输的中文就不会乱码了
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");

    try {
        username = request.getParameter("username");
        password = request.getParameter("password");
    } catch (Exception e) {
        username = "";
        password = "";
        e.printStackTrace();
    }

    //仅有js代码不足以阻止页面跳转（似乎），所以我加上了无username不能进入第二页面的设置
    if (!("123".equals(password)) || username.equals("")) {
        response.sendRedirect("index.jsp");
    }
    session.setAttribute("username", username);
    session.setAttribute("password", password);
%>
<h1>
    聊天室<br>
</h1>
<hr color="red">
<h2>
    用户
    <%
        out.print(username + "欢迎你!");
    %>
</h2>
<br>
<table border="1" style="vertical-align: middle" width="100%">
    <tr>
        <form method="post" action="index03.jsp">
            <th colspan="2">请输入留言</th>
            <th colspan="4"><input type="text" name="t"></th>
            <th colspan="1"><input type="submit" value="发布"></th>
        </form>
    </tr>
    <tr style="color: red">
        <th colspan="7">欢迎来到聊天室!</th>
    </tr>

    <%
        //这里模仿上面的显示格式
        if (application.getAttribute("say") == null) {
            System.out.println("没有say内容");
        } else {
            ArrayList<String> a = (ArrayList<String>) application.getAttribute("say");
            //这个不懂就写简单的for循环,截至到List的长度
            for (String s : a) {
                out.write("   <tr style=\"color: #56bec7\">\n" +
                        "        <th colspan=\"7\">" + s + "</th>\n" +
                        "    </tr>");
            }
        }
    %>


</table>

</body>
</html>
